package pers.litao.bit.user.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.litao.bit.commons.entities.user.InformationEntity;
import pers.litao.bit.commons.entities.user.UserInfoEntity;
import pers.litao.bit.commons.utils.*;
import pers.litao.bit.commons.vo.user.InformationVo;
import pers.litao.bit.user.dao.InformationMapper;
import pers.litao.bit.user.service.InformationService;
import pers.litao.bit.user.service.UserInfoService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 李涛
 * @date 2021-01-03 13:58
 */
@Service
@Slf4j
public class InformationServiceImpl extends ServiceImpl<InformationMapper, InformationEntity> implements InformationService {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 保存资讯
     *
     * @param informationEntity
     * @return
     */
    @Override
    public R saveInformation(InformationEntity informationEntity, LoginUserInfo loginUserInfo) {
        informationEntity.setCreateTime(DateUtil.date().getTime());
        informationEntity.setSellerId(loginUserInfo.getUserId());
        log.info("用户保存资讯"+loginUserInfo.info()+ SysUtil.getClientInfo().buildInfo());
        this.save(informationEntity);
        return R.ok();
    }

    /**
     * 资讯列表
     *
     * @param page
     * @return
     */
    @Override
    public R getList(Long page) {
        Page<InformationEntity> listPage = new Page<>();
        listPage.setSize(Constant.INFORMATION_SIZE);
        listPage.setCurrent(page);
        this.baseMapper.selectPage(listPage, new QueryWrapper<InformationEntity>()
        .orderByDesc("create_time"));
        List<InformationVo> collect = listPage.getRecords().stream()
                .map(m -> {
                    UserInfoEntity userInfoEntity = userInfoService.getBaseMapper().selectById(m.getSellerId());
                    InformationVo informationVo = new InformationVo();
                    informationVo.setContext(m.getContext());
                    informationVo.setName(userInfoEntity.getName());
                    informationVo.setTitle(m.getTitle());
                    informationVo.setTime(DateUtil.formatDate(DateUtil.date(m.getCreateTime())));
                    informationVo.setId(m.getId());
                    return informationVo;
                })
                .collect(Collectors.toList());
        Map<String,Object> map = new HashMap<>();
        // 是否还有下一页
        map.put("hasNext", listPage.hasNext());
        map.put("data", collect);
        log.info("用户请求成功" + SysUtil.getClientInfo().buildInfo());
        return R.ok(map);
    }

    @Override
    public R init() {
        InformationEntity informationEntity = this.baseMapper.selectById(2L);
        informationEntity.setId(null);
        System.out.println("开始保存");
        List<InformationEntity> list = new ArrayList<>();
        for(int i = 0;i < 30;i++){
            list.add(informationEntity);
        }
        this.saveBatch(list);
        System.out.println("结束保存");
        return R.ok();
    }
}
